package com.app.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.richfaces.component.UIDataTable;

import com.app.interfaces.interfaceService;
import com.app.model.Personnel;
import com.app.model.Service;

public class PersonnelBean extends MessageBean implements Serializable {
	private Personnel pers;
    private interfaceService persService;
    private interfaceService servService;

    private List person_list = new ArrayList();
    private List service_list = new ArrayList();
    private Hashtable servlist = new Hashtable();
    private String current_service;
    private boolean init;
    private Integer index = 0;
    private String message;
    private boolean nouveau;
    private UIDataTable dataTable;

    public void viderChamps(){
        message = "";
        changerEtat();
        this.pers = new Personnel();
        nouveau = true;
    }

    public void annuler(){
        message = "";
        person_list = (List)getPerson_list();
        pers = (Personnel)person_list.get(index);
        changerEtat();
    }

    public void chargerCombo(){
        current_service = this.pers.getService().getServCode() +"-"+ this.pers.getService().getServLib();
    }

    /** Creates a new instance of PersonnelBean */
    public PersonnelBean() {
        this.pers = new Personnel();
        this.pers.setService(new Service());
    }
    
    public void create(){
        if(nouveau){
            try{
                this.pers.setService((Service)servlist.get(current_service));
                this.persService.save(this.pers);
                message = mess_insert_true;
                style_message = "valid_message";
                chargerCombo();
            }catch(Exception e){
                e.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        } else {
            try{
                this.pers.setService((Service)servlist.get(current_service));
                this.persService.modify(pers);
                message = mess_modif_true;
                style_message = "valid_message";
                chargerCombo();
            }catch(Exception e){
                e.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        }
        changerEtat();
    }
    
    public void modifierLigne(){
        this.pers = (Personnel)dataTable.getRowData();
        chargerCombo();
        message = "";
        nouveau = false;
        changerEtat();
    }
    
    public void modifier(){
        message = "";
        nouveau = false;
        changerEtat();
    }
    
    public void supprimer(){
        try{
            this.persService.delete(pers);
            message = mess_del_true;
            style_message = "valid_message";
            if(index > 0){
                index--;
            }
            person_list = (List)getPerson_list();
            if(!person_list.isEmpty()){
                pers = (Personnel)person_list.get(index);
                chargerCombo();
            }else {
                viderChamps();
                changerEtat();
            }
        }catch(Exception e){
            e.printStackTrace();
            message = mess_op_false;
        }
    }

    public String getCurrent_service() {
        return current_service;
    }

    public UIDataTable getDataTable() {
        return dataTable;
    }

    public Integer getIndex() {
        return index;
    }

    public boolean isInit() {
        message = "";
        getServlist();
        getPerson_list();
        if(!(person_list.isEmpty())){
            person_list = (List)getPerson_list();
            pers = (Personnel) person_list.get(0);
            chargerCombo();
        }
        etat = true;
        invetat = !etat;
        return init;
    }

    public String getMessage() {
        return message;
    }

    public boolean isNouveau() {
        return nouveau;
    }

    public Personnel getPers() {
        return pers;
    }

    public interfaceService getPersService() {
        return persService;
    }

    public List getPerson_list() {
        person_list = this.persService.findAll();
        return person_list;
    }

    public interfaceService getServService() {
        return servService;
    }

    public List getService_list() {
        return service_list;
    }

    public Hashtable getServlist() {
        service_list.clear();
        servlist.clear();
        List l = this.servService.findAll();
        for(Iterator it = l.iterator(); it.hasNext();){
            Service srv = (Service) l.get(l.indexOf(it.next()));
            service_list.add(srv.getServCode()+ "-" + srv.getServLib());
            servlist.put(srv.getServCode() + "-" + srv.getServLib(), srv);
        }
        return servlist;
    }

    public void setCurrent_service(String current_service) {
        this.current_service = current_service;
    }

    public void setDataTable(UIDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public void setInit(boolean init) {
        this.init = init;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setNouveau(boolean nouveau) {
        this.nouveau = nouveau;
    }

    public void setPers(Personnel pers) {
        this.pers = pers;
    }

    public void setPersService(interfaceService persService) {
        this.persService = persService;
    }

    public void setPerson_list(List person_list) {
        this.person_list = person_list;
    }

    public void setServService(interfaceService servService) {
        this.servService = servService;
    }

    public void setService_list(List service_list) {
        this.service_list = service_list;
    }

    public void setServlist(Hashtable servlist) {
        this.servlist = servlist;
    }
}
